package com.canaan.business.controller.pc.storehouse;

import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusExportEnum;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.WmsStorehouseStockVo;
import com.canaan.business.service.inOutStock.IStorehouseStockCheckService;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库存Controller
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@Api(tags = "库存管理")
@RestController
@RequestMapping("/storehouse/stock")
public class WmsStorehouseStockController extends BaseController
{
    @Autowired
    private IWmsStorehouseStockService wmsStorehouseStockService;
    @Resource
    private IStorehouseStockCheckService storehouseStockCheckService;

    /**
     * 查询库存列表
     */
    @ApiOperation("查询库存列表")
    @GetMapping("/list")
    public TableDataInfo list(WmsStorehouseStock wmsStorehouseStock)
    {
        startPage();
        List<WmsStorehouseStock> list = wmsStorehouseStockService.selectNewWmsStorehouseStockListPageData(wmsStorehouseStock);
        return getDataTable(list);
    }

    /**
     * 查询库存物料下拉列表
     */
    @ApiOperation("查询库存物料作为下拉列表")
    @GetMapping("/selectionProductNoList")
    public List<WmsStorehouseStock> selectionProductNoList(WmsStorehouseStock wmsStorehouseStock)
    {
        List<WmsStorehouseStock> storehouseStockList = wmsStorehouseStockService.selectNewWmsStorehouseStockListPageData(wmsStorehouseStock);
        //storehouseStockList.add(new WmsStorehouseStock());
        return storehouseStockList;
    }

    /**
     * 导出库存列表
     */
    @ApiOperation(value = "导出库存列表")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStorehouseStock wmsStorehouseStock)
    {
        List<WmsStorehouseStock> list = wmsStorehouseStockService.selectNewWmsStorehouseStockListPageData(wmsStorehouseStock);
        // 循环把库存状态转为中文
        for (WmsStorehouseStock storehouseStock : list) {
            storehouseStock.setStatus(StorehouseStockStatusExportEnum.getMessageByCode(storehouseStock.getStatus()));
        }
        ExcelUtil<WmsStorehouseStock> util = new ExcelUtil<WmsStorehouseStock>(WmsStorehouseStock.class);
        util.exportExcel(response, list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @ApiOperation("获取库存详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsStorehouseStockService.selectWmsStorehouseStockById(id));
    }

    /**
     * 新增库存
     */
    @ApiOperation("新增库存")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsStorehouseStock wmsStorehouseStock)
    {
        return toAjax(wmsStorehouseStockService.insertWmsStorehouseStock(wmsStorehouseStock));
    }

    /**
     * 修改库存
     */
    @ApiOperation("修改库存")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsStorehouseStock wmsStorehouseStock)
    {
        return toAjax(wmsStorehouseStockService.updateWmsStorehouseStock(wmsStorehouseStock));
    }

    /**
     * 删除库存
     */
    @ApiOperation(value = "删除库存")
    @Log(title = "库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsStorehouseStockService.deleteWmsStorehouseStockByIds(ids));
    }

    /**
     * 获取盘点的库存条码信息
     */
    @ApiOperation("根据条码获取盘点的库存条码信息")
    @GetMapping(value = "/getCheckByBarcode/{barcode}")
    public AjaxResult getCheckByBarcode(@PathVariable("barcode") String barcode)
    {
        return success(wmsStorehouseStockService.getCheckByBarcode(barcode));
    }
    /**
     * 根据条码获取库存信息
     */
    @ApiOperation("根据条码获取库存信息")
    @GetMapping(value = "/infoByBarcode/{barcode}")
    public AjaxResult getInfoByBarcode(@PathVariable("barcode") String barcode)
    {
        return success(wmsStorehouseStockService.getInfoByBarcode(barcode));
    }

    /**
     * 根据库位获取库存信息
     */
    @ApiOperation("根据库位获取库存信息")
    @GetMapping(value = "/infoByLocationNo/{locationNo}")
    public AjaxResult getInfoByLocationNo(@PathVariable("locationNo") String locationNo)
    {
        return success(wmsStorehouseStockService.getInfoByLocationNo(locationNo));
    }



    /**
     * 导入仓库列表
     */
    @ApiOperation(value = "导入仓库列表")
    @PostMapping("/importExcel")
    @Log(title = "仓库", businessType = BusinessType.IMPORT)
    public AjaxResult importExcel(MultipartFile file) throws Exception {
        ExcelUtil<WmsStorehouseStock> util = new ExcelUtil<>(WmsStorehouseStock.class);
        List<WmsStorehouseStock> fileDataList = util.importExcel(file.getInputStream());
        String message = this.wmsStorehouseStockService.importData(fileDataList);
        return success(message);
    }
    /**
     * 根据库位获取库存信息
     */
    @ApiOperation("根据物料获取库存批次号")
    @GetMapping(value = "/getBatchNoListByProductNo/{productNo}")
    public AjaxResult getBatchNoListByProductNo(@PathVariable("productNo") String productNo)
    {
        return success(wmsStorehouseStockService.selectWmsStorehouseStockBatchNoByProductNo(productNo));
    }

    /**
     * 新增库存
     */
    @ApiOperation("绑定料架")
    @Log(title = "绑定料架", businessType = BusinessType.UPDATE)
    @PostMapping("/bindContainerNo")
    public AjaxResult bindContainerNo(@RequestBody WmsStorehouseStockVo vo)
    {
        return success(wmsStorehouseStockService.bindContainerNo(vo));
    }

    /**
     * 解绑库位料架
     */
    @ApiOperation("解绑库位料架")
    @Log(title = "解绑库位料架", businessType = BusinessType.UPDATE)
    @PostMapping("/unbindContainerNo")
    public AjaxResult unbindContainerNo(@RequestBody WmsStorehouseStockVo vo)
    {
        return wmsStorehouseStockService.unbindContainerNo(vo);
    }

    /**
     * 解绑托盘
     */
    @ApiOperation("解绑托盘")
    @Log(title = "解绑托盘", businessType = BusinessType.UPDATE)
    @PostMapping("/unbindTrayNo")
    public AjaxResult unbindTrayNo(@RequestBody WmsStorehouseStockVo vo)
    {
        return wmsStorehouseStockService.unbindTrayNo(vo);
    }

    /**
     * 根据货架号查询对应信息
     */
    @ApiOperation("根据货架号查询对应信息")
    @GetMapping(value = "/selectStockInfosByContainerNo")
    public AjaxResult selectStockInfosByContainerNo(@RequestParam("containerNo") String containerNo) {
        return success(wmsStorehouseStockService.selectStockInfosByContainerNo(containerNo));
    }
}
